<?php

/**
 * @author Martin Pernica
 * @copyright 2008
 */

if (!empty($_POST['vote'])) {
	
	$getCurrentVote = Registry::get('db')->query("SELECT vote, ip FROM {prefix}poll WHERE id = '" . intval($q[1]) . "' LIMIT 1");
    $data = $getCurrentVote->fetch_array();
	$uip = $visitors_online->ipCheck();
	if($uip == false){
		$uip = '00.000.000.000';
	}
    $ip = unserialize($data['ip']);
    
    if (array_search($visitors_online->ipCheck(), (array)$ip, true)) {
        echo '<li><strong>Již jste hlasoval !</strong></li>';
    } else {
    
    $currentVote = unserialize($data['vote']);
    $answer = $currentVote[$_POST['vote']];
    $answer++;

	array_push($ip, $uip);
    $ip = serialize($ip);
    
    $currentVote[$_POST['vote']] = $answer;
    $finish = serialize($currentVote);

    $query = Registry::get('db')->query("UPDATE {prefix}poll SET vote = '" . $finish . "', ip = '" . $ip . "' WHERE id = '".intval($q[1])."'");
    $log->write(8, 'com_poll', 'NOW()', $visitors_online->ipCheck().' voted to poll #'.intval($q[1]), 0);
    Text::h2( Lang::getFront('com_poll', 'voted') );
    echo '<meta http-equiv="refresh" content="2;url={url}index.php?q=poll">';
	}
} else
    if (intval($q[1])) {
        $query = Registry::get('db')->query("SELECT * FROM {prefix}poll WHERE id = '" . intval($q[1]) .
            "' AND active = '1' ORDER BY id LIMIT 1");
        $data = $query->fetch_array();
        echo '<h2>'.Lang::getFront('com_poll', 'poll').' <em>' . $data['question'] . '</em></h2>';
        echo '<table>';
        echo '<tr><th>' . $data['question'] . '</th></tr>';
        $vote = unserialize($data['vote']);
        $answers = unserialize($data['answers']);
        foreach ($answers as $key => $val) {
            echo '<tr><td>' . $val . '</td><td>' . $vote[$key] . '</td></tr>';
        }
        echo '</table>';
    } else {
        $query = Registry::get('db')->query("SELECT id, question FROM {prefix}poll WHERE active = '1' ORDER BY id LIMIT 50");
        Text::h2( Lang::getFront('com_poll', 'list') );
        while ($data = $query->fetch_array()) {
            echo '<table>';
            echo '<tr><th><a href="{url}index.php?q=poll/' . $data['id'] . '">' . $data['question'] .
                '</a></th></tr>';
            echo '</table>';
        }
    }

?>